/*
 * @Author: Xujianchen
 * @Date: 2024-06-28 09:33:01
 * @LastEditors: xujianchen
 * @LastEditTime: 2024-06-28 09:33:17
 * @Description: 网络是否可用
 */
import { isBoolean } from '@/utils/type'

export default function useOnline() {
  const online = ref(true)

  navigator.onLine ? showStatus(true) : showStatus(false)

  onMounted(() => {
    window.addEventListener('online', showStatus)
    window.addEventListener('offline', showStatus)
  })

  onUnmounted(() => {
    window.removeEventListener('online', showStatus)
    window.removeEventListener('offline', showStatus)
  })

  function showStatus(val) {
    online.value = isBoolean(val) ? val : val.target.online
  }

  return {
    online,
  }
}
